<?php
/**
 * thumbnail.libaray.php
 * @Copyright lemonCarlJ All rights reserved.
 * @License http://www.56code.com/License.txt
 * @Version 1.0.0
 * @Email rpgshifa@aliyun.com
 */
namespace SF\libaray;

class thumbnail
{
    /**
     * 略缩图的宽
     * @var int
     */
    public $width;
    
    /**
     * 略缩图的高
     * @var int
     */
    public $height;
    
    /**
     * 是否对图片进行处理
     * @var boolean
     */
    public $handle = true;
    
    /**
     * 处理图片时保持图片完整性
     * @var boolean
     */
    public $intact = false;
    
    /**
     * 图片质量
     * @var int
     */
    public $quality = 100;
    
    /**
     * 优先截取位置
     * @var int
     * @desc 截取位置：1为前或上 0为中间 -1为后或下
     */
    public $locate = 0;
    
    /**
     * 背景色
     * @var array
     */
    public $background = array( 255, 255, 255 );
    
    /**
     * 创建略缩图
     * @param string $source_pic 源图片地址
     * @param string $target_pic 目标图片地址
     * @return boolean 返回是否创建成功
     */
    public function create( $source_pic, $target_pic )
    {
        //源图信息
        if( ! ( $source_info = getimagesize( $source_pic ) ) )
        {
            return false;
        }
        
        //是否存在保存操作函数
        $type = image_type_to_extension( $source_info[ 2 ], false );
        $image_function = 'image' . $type;
        if( ! function_exists( $image_function ) )
        {
            return false;
        }
        
        //是否有源图片文件
        if( ! file_exists( $source_pic ) )
        {
            return false;
        }
        
        //略缩图宽高
        $picture_width = $this->width;
        $picture_height = $this->height;
        
        //创建略缩图
        $picture_image = imagecreatetruecolor( $picture_width, $picture_height );
        
        //填充背景色
        $background = $this->background;
        $background_color = imagecolorallocate( $picture_image, $background[ 0 ], $background[ 1 ], $background[ 2 ] );
        imagefill( $picture_image, 0, 0, $background_color);
        
        //源图宽高
        $source_width = $source_info[ 0 ];
        $source_height = $source_info[ 1 ];
        
        //初始化截取位置
        $source_x_start = 0;
        $source_y_start = 0;
        $source_x_end   = $source_width;
        $source_y_end   = $source_height;
        
        //初始化插入位置
        $picture_x_start = 0;
        $picture_y_start = 0;
        
        //截取宽高
        $picture_clip_width = $picture_width;
        $picture_clip_height = $picture_height;
        
        //略缩图宽高比
        $picture_ratio = $picture_width / $picture_height;
        
        //源图片宽高比
        $source_ratio = $source_width / $source_height;
        
        //位置
        $locate = $this->locate;
                
        //是否对图片优化处理
        if( $this->handle )
        {
            if( $this->intact )
            {
                //同比例略缩图宽多
                if( $picture_ratio > $source_ratio )
                {
                    $picture_clip_width = $picture_height * $source_ratio;
                    
                    //宽差距
                    $width_gap = $picture_width - $picture_clip_width;
                    
                    //略缩图X写入点
                    $picture_x_start = $width_gap / 2;
                }
                
                //同比例略缩图高多
                if( $picture_ratio < $source_ratio )
                {
                    $picture_clip_height = $picture_width * ( $source_height / $source_width );
                    
                    //高差距
                    $height_gap = $picture_height - $picture_clip_height;
                    
                    //略缩图Y写入点
                    $picture_y_start = $height_gap / 2;
                }
            }else{
                //源图宽少高少
                if( ( $picture_width > $source_width ) && ( $picture_height > $source_height ) )
                {
                    //略缩图X写入点
                    $picture_x_start = ( $picture_width - $source_width ) / 2;
                    
                    //略缩图Y写入点
                    $picture_y_start = ( $picture_height - $source_height ) / 2;
                    
                    //宽高差距
                    $width_gap = $picture_width - $source_width;
                    $height_gap = $picture_height - $source_height;
                    
                    //截取
                    $picture_clip_width -= $width_gap;
                    $picture_clip_height -= $height_gap;
                }
                
                //源图宽多高多
                if( ( $picture_width < $source_width ) && ( $picture_height < $source_height ) )
                {
                    //使用固定切法
                    
                    /**
                    $source_x_start = ( $source_width - $picture_width ) / 2;
                    $source_y_start = ( $source_height - $picture_height ) / 2;
                    
                    $source_x_end = $picture_width;
                    $source_y_end = $picture_height;
                    **/
                    
                    //显示最大化切法
                    
                    //同比例略缩图宽多
                    if( $picture_ratio > $source_ratio )
                    {
                        $picture_clip_width = $source_width * ( $picture_width / $source_width );
                        $picture_clip_height = $source_height * ( $picture_width / $source_width );
                        
                        //略缩图Y写入点
                        if( $locate == 1 )
                        {
                            $source_y_start = 0;
                        }elseif( $locate == -1 ){
                            $source_y_start = floor( $picture_clip_height - $picture_height ) / ( $picture_width / $source_width );
                        }else{
                            $source_y_start = ( $source_height - $picture_height ) / 2;
                        }
                    }
                    
                    //同比例略缩图高多
                    if( $picture_ratio < $source_ratio )
                    {
                        $picture_clip_width = $source_width * ( $picture_height / $source_height );
                        $picture_clip_height = $source_height * ( $picture_height / $source_height );
                        
                        //略缩图X写入点
                        if( $locate == 1 )
                        {
                            $source_x_start = 0;
                        }elseif( $locate == -1 ){
                            $source_x_start = floor( $picture_clip_width - $picture_width ) / ( $picture_height / $source_height );
                        }else{
                            $source_x_start = ( $source_width - $picture_width ) / 2;
                        }
                    }
                    
                }
                
                //源图宽多高少(等)
                if( ( $picture_width < $source_width ) && ( ( $picture_height > $source_height ) || ( $picture_height == $source_height ) ) )
                {
                    //高差距
                    $height_gap = $picture_height - $source_height;
                    
                    //略缩图Y写入点
                    $picture_y_start = $height_gap / 2;
                    
                    //截取高度
                    $picture_clip_height -= $height_gap;
                    
                    //源图Y截取位置
                    if( $locate == 1 )
                    {
                        $source_x_end = $picture_width;
                    }elseif( $locate == -1 ){
                        $source_x_start = $source_width - $picture_width;
                        $source_x_end = $picture_width;
                    }else{
                        $source_x_start = ( $source_width - $picture_width ) / 2;
                        $source_x_end = $picture_width;
                    }
                }
                
                //源图宽少(等)高多
                if( ( ( $picture_width > $source_width ) || ( $picture_width == $source_width ) ) && ( $picture_height < $source_height ) )
                {
                    //宽差距
                    $width_gap = $picture_width - $source_width;
                    
                    //略缩图X写入点
                    $picture_x_start = $width_gap / 2;
                    
                    //截取宽度
                    $picture_clip_width -= $width_gap;
                    
                    //源图X截取位置
                    if( $locate == 1 )
                    {
                        $source_y_end = $picture_height;
                    }elseif( $locate == -1 ){
                        $source_y_start = $source_height - $picture_height;
                        $source_y_end = $picture_height;
                    }else{
                        $source_y_start = ( $source_height - $picture_height ) / 2;
                        $source_y_end = $picture_height;
                    }
                }
            }
        }
        
        //读取源图
        $source_pic_image = imagecreatefromstring( file_get_contents( $source_pic ) );
        
        //合并图片生成略缩图
        imagecopyresampled( $picture_image, $source_pic_image, $picture_x_start, $picture_y_start, $source_x_start, $source_y_start, $picture_clip_width, $picture_clip_height, $source_x_end, $source_y_end );
        
        //生成图片,jpeg可设置图片质量
        if( $type == 'jpeg' )
        {
            $result = $image_function( $picture_image, $target_pic, $this->quality );
        }else{
            $result = $image_function( $picture_image, $target_pic );
        }
        
        //销毁图片
        imagedestroy( $source_pic_image );
        imagedestroy( $picture_image );
        
        return $result;
    }
}